{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5ecec251",
   "metadata": {},
   "source": [
    "## 列表切片的使用"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce53fd4e",
   "metadata": {},
   "source": [
    "### 练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "5345a84b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['d', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "phrase = \"don't panic!\"\n",
    "plist = list(phrase)\n",
    "new_phrase = ''.join(plist[1:3])        # 首先从plist中取出单词on\n",
    "new_phrase = new_phrase+''.join([plist[5],plist[4],plist[7],plist[6]])\n",
    "                          # 然后挑出我们需要的各个其他字母：空格，”t“，”a“，”p\"\n",
    "print(plist)\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d7670f6",
   "metadata": {},
   "source": [
    "### “for”循环了解列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6cee39a1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\t M\n",
      "\t a\n",
      "\t r\n",
      "\t v\n",
      "\t i\n",
      "\t n\n"
     ]
    }
   ],
   "source": [
    "paranoid_android = 'Marvin'\n",
    "letters = list(paranoid_android)\n",
    "for char in letters:            \n",
    "    print('\\t',char)            # '/t'会让各个字符分行打印"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c69a49d",
   "metadata": {},
   "source": [
    "### python的“for”循环了解切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "5b852d3f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\t M\n",
      "\t a\n",
      "\t r\n",
      "\t v\n",
      "\t i\n",
      "\t n\n",
      "\n",
      "\t\t a\n",
      "\t\t n\n",
      "\t\t d\n",
      "\t\t r\n",
      "\t\t o\n",
      "\t\t i\n",
      "\t\t d\n",
      "\n",
      "\t\t\t a\n",
      "\t\t\t r\n",
      "\t\t\t a\n",
      "\t\t\t n\n",
      "\t\t\t o\n",
      "\t\t\t i\n",
      "\t\t\t d\n",
      "\t\t\t  \n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 使用中括号从列表在选择一个切片，for循环只迭代处理切片部分的对象\n",
    "paranoid_android = 'Marvin,the paranoid android'\n",
    "letters = list(paranoid_android)\n",
    "for char in letters[:6]:            # 循环迭代处理列表中前6个对象的一个切片\n",
    "    print('\\t',char)\n",
    "print()\n",
    "for char in letters[-7:]:           # 循环迭代处理列表中后7个对象的一个切片\n",
    "    print('\\t'*2,char)               # ”*2“会在每个要打印的对象前面插入两个制表符\n",
    "print()\n",
    "for char in letters[12:20]:\n",
    "    print('\\t'* 3, char)\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3caf0114",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
